﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class FormCadastroPatio : Form
    {
        private string operacao = "";

        public FormCadastroPatio()
        {
            InitializeComponent();
        }

        private void FormCadastroPatio_Load(object sender, EventArgs e)
        {
            //Popular a lista de patios cadastrados
            this.dataGridViewPatios.DataSource = ModelBO.getPatios();
            setupButtonsControls();
        }

        private void dataGridViewPatio_SelectionChanged(object sender, EventArgs e)
        {
            setupButtonsControls();
        }

        private void buttonNovo_Click(object sender, EventArgs e)
        {
            this.operacao = "novo";
            setupButtonsControls();
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.operacao = "cancelar";
            setupButtonsControls();
        }

        private void buttonEditar_Click(object sender, EventArgs e)
        {
            this.operacao = "editar";
            setupButtonsControls();
        }

        private void buttonExcluir_Click(object sender, EventArgs e)
        {
            List<PatioVO> patioList = (List<PatioVO>)this.dataGridViewPatios.DataSource;
            PatioVO patio = patioList[this.dataGridViewPatios.CurrentRow.Index];

            if (!ModelBO.readyToDeletePatio(patio.idPatio))
            {
                MessageBox.Show(this, "Patio não pode ser excluído pois já está associado a Cabines", "Aviso");
                return;
            }

            this.operacao = "excluir";

            DialogResult dr = MessageBox.Show(this, "Confirma a exclusão do pátio " + patio.patio, "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString().Equals("Yes"))
            {
                ModelBO.delPatio(patio);

                int nRow = this.dataGridViewPatios.CurrentRow.Index;
                this.dataGridViewPatios.DataSource = ModelBO.getPatios();
                if (this.dataGridViewPatios.Rows.Count > 0)
                {
                    dataGridViewPatios.CurrentCell = dataGridViewPatios.Rows[Math.Min(nRow, this.dataGridViewPatios.Rows.Count - 1)].Cells[1];
                    dataGridViewPatios.Rows[Math.Min(nRow, this.dataGridViewPatios.Rows.Count - 1)].Selected = true;
                }

                setupButtonsControls();
            }
        }

        private void buttonSalvar_Click(object sender, EventArgs e)
        {
            String erros = "";
            if (this.textBoxPatio.Text.Trim().Length == 0)
                erros = erros + "Patio deve ser informado.\r\n";
            if (erros.Length > 0)
            {
                MessageBox.Show(this, erros, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            PatioVO patio = null;

            if (this.operacao.Equals("novo"))
            {

                patio = PatioVO.CreatePatioVO();
                patio.patio = this.textBoxPatio.Text;

                patio.idPatio = (int)ModelBO.newPatio(patio);
            }
            if (this.operacao.Equals("editar"))
            {
                List<PatioVO> patioList = (List<PatioVO>)this.dataGridViewPatios.DataSource;
                patio = patioList[this.dataGridViewPatios.CurrentRow.Index];

                patio.patio = this.textBoxPatio.Text;

                ModelBO.setPatio(patio);
            }
            this.operacao = "salvar";

            List<PatioVO> listPatioVOs = ModelBO.getPatios();
            dataGridViewPatios.DataSource = listPatioVOs;
            int nRow = listPatioVOs.FindIndex(a => a.idPatio == patio.idPatio);
            dataGridViewPatios.CurrentCell = dataGridViewPatios.Rows[nRow].Cells[1];
            dataGridViewPatios.Rows[nRow].Selected = true;

            setupButtonsControls();
        }


        private void setupButtonsControls()
        {
            bool haRows = this.dataGridViewPatios.Rows.Count > 0 && this.dataGridViewPatios.CurrentRow!=null;
            bool novo = this.operacao.Equals("novo");
            bool edit = this.operacao.Equals("editar");
            bool canc = this.operacao.Equals("cancelar");

            this.buttonCancelar.Enabled = novo || edit;
            this.buttonNovo.Enabled = !novo && !edit;
            this.buttonEditar.Enabled = haRows && !novo && !edit;
            this.buttonExcluir.Enabled = haRows && !novo && !edit;
            this.buttonSalvar.Enabled = novo || edit;

            this.textBoxPatio.Enabled = novo || edit;

            this.dataGridViewPatios.Enabled = !novo && !edit;

            if (!haRows || novo)
            {
                this.textBoxId.Text = "";
                this.textBoxPatio.Text = "";
            }
            else {
                List<PatioVO> patioList = (List<PatioVO>)this.dataGridViewPatios.DataSource;
                PatioVO patio = patioList[this.dataGridViewPatios.CurrentRow.Index];
                this.textBoxId.Text = patio.idPatio.ToString();
                this.textBoxPatio.Text = patio.patio;
            }
            if (novo || edit){
                this.textBoxPatio.Focus();
            }
        }

        private void buttonListagem_Click(object sender, EventArgs e)
        {
            FormReportPatio formx = new FormReportPatio();
            formx.MdiParent = this.MdiParent;
            formx.Show();

        }

     }
}
